package com.pfzy.ui.activity.dragsort;

import android.graphics.Canvas;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.Collections;

/**
 * @author CJF
 * 列表上下左右拖拽滑动
 */
public class AdapterTouchHelper2 extends ItemTouchHelper.Callback {
    private BaseQuickAdapter adapter;

    public AdapterTouchHelper2(RecyclerView recyclerView, BaseQuickAdapter adapter) {
        this.adapter = adapter;
        // 用Callback构造ItemtouchHelper
        ItemTouchHelper touchHelper = new ItemTouchHelper(this);
        // 调用ItemTouchHelper的attachToRecyclerView方法建立联系
        touchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public int getMovementFlags(@NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder) {
        //允许上下拖动
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        return makeMovementFlags(dragFlags, 0);
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    /**
     * Item进行上下移动从旧的位置到新的位置的时候会调用该方法
     *
     * @param recyclerView
     * @param viewHolder   旧位置
     * @param target       目标位置
     * @return
     */
    @Override
    public boolean onMove(@NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder, @NotNull RecyclerView.ViewHolder target) {
        // 交换位置
        Collections.swap(adapter.getData(), viewHolder.getAdapterPosition(), target.getAdapterPosition());
        adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    /**
     * Item左右移动达到条件时时，会调用该方法
     *
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(@NotNull RecyclerView.ViewHolder viewHolder, int direction) { }

    /**
     * 重写onChildDraw 防止越界拖动
     *
     * @param c
     * @param recyclerView
     * @param viewHolder
     * @param dX
     * @param dY
     * @param actionState
     * @param isCurrentlyActive
     */
    @Override
    public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        float topY = viewHolder.itemView.getTop() + dY;
        float bottomY = topY + viewHolder.itemView.getHeight();

        float leftX = viewHolder.itemView.getLeft() + dX;
        float rightX = leftX + viewHolder.itemView.getWidth();

        if (topY < 0) {
            dY = 0;
        } else if (bottomY > recyclerView.getHeight()) {
            dY = recyclerView.getHeight() - viewHolder.itemView.getHeight() - viewHolder.itemView.getTop();
        }

        if (leftX < 0) {
            dX = viewHolder.itemView.getWidth() - viewHolder.itemView.getRight();
        } else if (rightX > recyclerView.getWidth()) {
            dX = recyclerView.getWidth() - viewHolder.itemView.getWidth() - viewHolder.itemView.getLeft();
        }

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //开始按住滑动
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            //按住变大
            viewHolder.itemView.setScaleX(1.2f);
            viewHolder.itemView.setScaleY(1.2f);
            viewHolder.itemView.setAlpha(0.5f);
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(@NotNull RecyclerView recyclerView, @NotNull RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        //结束按住滑动 变回原来大小
        viewHolder.itemView.setScaleX(1.0f);
        viewHolder.itemView.setScaleY(1.0f);
        viewHolder.itemView.setAlpha(1.0f);
    }
}